<?php

    class auth_model extends CI_Model {
    
        private $salt;
        
        public function __construct() {
            parent::__construct();
            $this->salt = "#4;DuKrM Yhl:2\l?Qe,SaTX}*z#J0jH";
        }
    
        public function check_user($meno, $heslo) { 
            //$query = $this->db->query("SELECT * FROM users WHERE meno='" . $meno . "' AND heslo='" . md5($this->salt . $heslo) . "'");
            $query = $this->db->select('*')->from('users')->where('meno', $meno)->where('heslo', md5($this->salt . $heslo))->get(); 
            if ( $query->num_rows() > 0) {
                return $query->row_array();
            }
            else {
                return FALSE;
            }
        }
        
        public function check_old_password($user_id, $old_password) {
            $query = $this->db->select('*')->from('users')->where('id', (int)$user_id)->where('heslo', md5($this->salt . $old_password))->get();
            if ($query->num_rows() == 1) {
                $query->free_result();
                return TRUE;
            } else {
                $query->free_result();
                return FALSE;
            }
        }
        
        public function change_password($user_id, $new_password) {
            $this->db->set('heslo', md5($this->salt . $new_password));
            $this->db->where('id', (int)$user_id);
            $this->db->update('users');
            if ($this->db->affected_rows() == 1) { 
                return TRUE;
            }
            return FALSE;
        }
    }
    
?>